package hanjia;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2025/2/11-下午3:21
 * Since  :   1.0
 * Version:   1.0
 */
public class LeetCode257 {

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> result = new ArrayList<>();
        List<Integer> paths = new ArrayList<>();

        return huisu(root, result, paths);


    }

    private List<String> huisu(TreeNode root, List<String> result, List<Integer> paths) {
        paths.add(root.val);
        if (root.left == null && root.right == null) {
            StringBuffer stringBuffer = new StringBuffer();
            for (int i : paths) {
                stringBuffer.append(i);
                stringBuffer.append("->");
            }
            String string = stringBuffer.toString();
            result.add(string.substring(0, string.length() - 2));
            return result;

        }


        if (root.left != null) {
            huisu(root.left, result, paths);
            paths.remove(paths.size() - 1);
        }
        if (root.right != null) {
            huisu(root.right, result, paths);
            paths.remove(paths.size() - 1);
        }

        return result;


    }


}
